package com.lanyun.oss.controller;

import cn.hutool.core.util.ObjectUtil;
import com.lanyun.oss.entity.File;
import com.lanyun.oss.service.FileService;
import com.lanyun.oss.service.StorageService;
import com.lanyun.web.bean.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @Author Jqcc
 * @Version 1.0
 * @Data 2025/3/14
 */
@RestController
@RequestMapping("/api-admin/file")
public class FileUploadController {

    @Resource
    private StorageService storageService;

    @Resource
    private FileService fileService;

    @PostMapping("/upload")
    public Result uploadFile(@RequestParam("file") MultipartFile file,
                             @RequestParam("productId") String productId,
                             @RequestParam("detailType") String detailType) {
        String filePath = storageService.uploadFile(file);
        fileService.insert(file, filePath, productId, detailType);
        return Result.success(filePath);
    }

    @PostMapping("/uploadSingle")
    public Result uploadSingle(@RequestParam("file") MultipartFile file) {
        String filePath = storageService.uploadFile(file);
        return Result.success(filePath);
    }

    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody File file) {
        Boolean result = fileService.delete(file);
        return Result.success(result);
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody File file) {
        File result = fileService.update(file);
        return Result.success(ObjectUtil.isNotNull(result));
    }
}
